#   request.py
#   Generates the query request and retrieves output using the API
#   example main commented out to show how to use the functions to get results

import cgi, os
import httplib
import urllib2
from xml.dom.minidom import parse, parseString

#specify API keys
appID = 'dhH7ncTvG547cdtC'

#given a query, returns the results XML created by the request
#called by functions that return results
def getResultsXML(searchType, keywords):
    #set up connection request
    url = 'http://api.eventful.com/rest/' + searchType + '/search?app_key=' + appID + '&keywords=' + keywords
    response = urllib2.urlopen(url).read()
    return response

#given a query, returns the results XML created by the request
#called by functions that return results
#includes location query
def getResultsXMLLocation(searchType, keywords, location):
    #set up connection request
    if keywords is '':
        url = 'http://api.eventful.com/rest/' + searchType + '/search?app_key=' + appID + '&keywords=' + keywords + '&location=' + location
    elif location is '':
        url = 'http://api.eventful.com/rest/' + searchType + '/search?app_key=' + appID + '&keywords=' + keywords
    else:
        url = 'http://api.eventful.com/rest/' + searchType + '/search?app_key=' + appID + '&location=' + location
    response = urllib2.urlopen(url).read()
    return response
    
#given a query, returns the results XML created by the request
#called by functions that return results
#includes location query
def getResultsXMLByID(searchType, id):
    #set up connection request
    url = 'http://api.evdb.com/rest/' + searchType + '/get?app_key=' + appID + '&id=' + id
    response = urllib2.urlopen(url).read()
    return response

#returns list of titles for events that match the query keywords and location
#for searching by just location, keywords = ''
#for searching by just keywords, location = ''
def getEventTitlesByLocation(keywords, location):
    titles = []
    data = getResultsXMLLocation('events', keywords, location)

    #parse the response data into a DOM
    response = parseString(data)
    searchResultsNode = response.getElementsByTagName('search')[0]
    numElems = int(searchResultsNode.getElementsByTagName('total_items')[0].childNodes[0].nodeValue)
    pageSize = int(searchResultsNode.getElementsByTagName('page_size')[0].childNodes[0].nodeValue)
    #eventsNode = getEvents(keywords)
    
    if numElems != 0:
        for i in range(pageSize):
            eventsNode = searchResultsNode.getElementsByTagName('events')[0]
            eventNode = eventsNode.getElementsByTagName('event')[i]
            
            viewTitleNode = eventNode.getElementsByTagName('title')[0]
            titles.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return titles
    
#returns list of titles for events that match the query keywords
#this function supports backwards compatibility with previous versions
def getEventTitles(keywords):
    return getEventTitlesByLocation(keywords, '')

#get venues of events that match query
def getEventVenueNameByLocation(keywords, location):
    names = []  
    data = getResultsXMLLocation('events', keywords, location)

    # parse the response data into a DOM
    response = parseString(data)
    searchResultsNode = response.getElementsByTagName('search')[0]
    numElems = int(searchResultsNode.getElementsByTagName('total_items')[0].childNodes[0].nodeValue)
    pageSize = int(searchResultsNode.getElementsByTagName('page_size')[0].childNodes[0].nodeValue)
    
    if numElems != 0:
        for i in range(pageSize):
            eventsNode = searchResultsNode.getElementsByTagName('events')[0]
            eventNode = eventsNode.getElementsByTagName('event')[i]
            
            viewNameNode = eventNode.getElementsByTagName('venue_name')[0]
            names.append(viewNameNode.childNodes[0].nodeValue)

    response.unlink
    return names
    
#returns list of titles for events that match the query keywords
#this function supports backwards compatibility with previous versions
def getEventVenueName(keywords):
    return getEventVenueNameByLocation(keywords, '')

#get venues of events that match query
def getEventDescriptionByLocation(keywords, location):
    descriptions = []  
    data = getResultsXMLLocation('events', keywords, location)

    # parse the response data into a DOM
    response = parseString(data)
    searchResultsNode = response.getElementsByTagName('search')[0]
    numElems = int(searchResultsNode.getElementsByTagName('total_items')[0].childNodes[0].nodeValue)
    pageSize = int(searchResultsNode.getElementsByTagName('page_size')[0].childNodes[0].nodeValue)
    
    if numElems != 0:
        for i in range(pageSize):
            eventsNode = searchResultsNode.getElementsByTagName('events')[0]
            eventNode = eventsNode.getElementsByTagName('event')[i]
            
            viewNameNode = eventNode.getElementsByTagName('description')[0]
            descriptions.append(viewNameNode.childNodes[0].nodeValue)

    response.unlink
    return descriptions
    #returns list of titles for events that match the query keywords

#this function supports backwards compatibility with previous versions
def getEventDescription(keywords):
    return getEventDescriptionByLocation(keywords, '')
    
#get venues of events that match query
def getEventStartTimeByLocation(keywords, location):
    times = []  
    data = getResultsXMLLocation('events', keywords, location)

    # parse the response data into a DOM
    response = parseString(data)
    searchResultsNode = response.getElementsByTagName('search')[0]
    numElems = int(searchResultsNode.getElementsByTagName('total_items')[0].childNodes[0].nodeValue)
    pageSize = int(searchResultsNode.getElementsByTagName('page_size')[0].childNodes[0].nodeValue)
    
    if numElems != 0:
        for i in range(pageSize):
            eventsNode = searchResultsNode.getElementsByTagName('events')[0]
            eventNode = eventsNode.getElementsByTagName('event')[i]
            
            viewNameNode = eventNode.getElementsByTagName('start_time')[0]
            times.append(viewNameNode.childNodes[0].nodeValue)

    response.unlink
    return times
    
#returns list of titles for events that match the query keywords
#this function supports backwards compatibility with previous versions
def getEventStartTime(keywords):
    return getEventStartTimeByLocation(keywords, '')
    
#get venues of events that match query
def getEventCityByLocation(keywords):
    cities = []  
    data = getResultsXMLLocation('events', keywords, location)

    # parse the response data into a DOM
    response = parseString(data)
    searchResultsNode = response.getElementsByTagName('search')[0]
    numElems = int(searchResultsNode.getElementsByTagName('total_items')[0].childNodes[0].nodeValue)
    pageSize = int(searchResultsNode.getElementsByTagName('page_size')[0].childNodes[0].nodeValue)
    
    if numElems != 0:
        for i in range(pageSize):
            eventsNode = searchResultsNode.getElementsByTagName('events')[0]
            eventNode = eventsNode.getElementsByTagName('event')[i]
            
            viewNameNode = eventNode.getElementsByTagName('city_name')[0]
            cities.append(viewNameNode.childNodes[0].nodeValue)

    response.unlink
    return cities
    
#returns list of titles for events that match the query keywords
#this function supports backwards compatibility with previous versions
def getEventCity(keywords):
    return getEventCityByLocation(keywords, '')
    
#get venues of events that match query
def getEventRegionByLocation(keywords):
    regions = []  
    data = getResultsXMLLocation('events', keywords, location)

    # parse the response data into a DOM
    response = parseString(data)
    searchResultsNode = response.getElementsByTagName('search')[0]
    numElems = int(searchResultsNode.getElementsByTagName('total_items')[0].childNodes[0].nodeValue)
    pageSize = int(searchResultsNode.getElementsByTagName('page_size')[0].childNodes[0].nodeValue)
    
    if numElems != 0:
        for i in range(pageSize):
            eventsNode = searchResultsNode.getElementsByTagName('events')[0]
            eventNode = eventsNode.getElementsByTagName('event')[i]
            
            viewNameNode = eventNode.getElementsByTagName('region_name')[0]
            regions.append(viewNameNode.childNodes[0].nodeValue)

    response.unlink
    return regions
    
#returns list of titles for events that match the query keywords
#this function supports backwards compatibility with previous versions
def getEventRegion(keywords):
    return getEventRegionByLocation(keywords, '')
    
#get venues of events that match query
def getEventIDs(keywords, location):
    eventCounts = []
    data = getResultsXMLLocation('events', keywords, location)

    # parse the response data into a DOM
    response = parseString(data)
    searchResultsNode = response.getElementsByTagName('search')[0]
    numElems = int(searchResultsNode.getElementsByTagName('total_items')[0].childNodes[0].nodeValue)
    pageSize = int(searchResultsNode.getElementsByTagName('page_size')[0].childNodes[0].nodeValue)
    
    if numElems != 0:
        for i in range(pageSize):
            eventsNode = searchResultsNode.getElementsByTagName('events')[0]
            eventNode = eventsNode.getElementsByTagName('event')[i]
            
            viewTitleNode = eventNode.attributes['id']
            eventCounts.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return eventCounts
    
#returns array of URLS of images
def getEventTitleByID(id):
    names = []
    data = getResultsXMLByID('events', id)
    # parse the response data into a DOM
    response = parseString(data)
    
    performerNode = response.getElementsByTagName('event')[0]
    viewTitleNode = performerNode.getElementsByTagName('title')[0]
    if len(viewTitleNode.childNodes) != 0:
        names.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return names
    
#returns array of URLS of images
def getEventDescriptionByID(id):
    names = []
    data = getResultsXMLByID('events', id)
    # parse the response data into a DOM
    response = parseString(data)
    
    performerNode = response.getElementsByTagName('event')[0]
    viewTitleNode = performerNode.getElementsByTagName('description')[0]
    if len(viewTitleNode.childNodes) != 0:
        names.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return names
    
#returns array of URLS of images
def getEventStartTimeByID(id):
    names = []
    data = getResultsXMLByID('events', id)
    # parse the response data into a DOM
    response = parseString(data)
    
    performerNode = response.getElementsByTagName('event')[0]
    viewTitleNode = performerNode.getElementsByTagName('start_time')[0]
    if len(viewTitleNode.childNodes) != 0:
        names.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return names

#returns array of URLS of images
def getEventVenueIDByID(id):
    names = []
    data = getResultsXMLByID('events', id)
    # parse the response data into a DOM
    response = parseString(data)
    
    performerNode = response.getElementsByTagName('event')[0]
    viewTitleNode = performerNode.getElementsByTagName('venue_id')[0]
    if len(viewTitleNode.childNodes) != 0:
        names.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return names
    
#returns array of URLS of images
def getEventImagesByID(id):
    names = []
    data = getResultsXMLByID('events', id)
    # parse the response data into a DOM
    response = parseString(data)
    
    performerNode = response.getElementsByTagName('event')[0]
    imagesNode = performerNode.getElementsByTagName('images')[0]
    for i in range(len(imagesNode.childNodes)):
        imageNode = imagesNode.getElementsByTagName('image')[i]
        viewTitleNode = performerNode.getElementsByTagName('url')[0]
        if len(viewTitleNode.childNodes) != 0:
            names.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return names
    
#get venue names by search query
def getVenueNameByLocation(keywords, location):
    names = []
    data = getResultsXMLLocation('venues', keywords, location)

    # parse the response data into a DOM
    response = parseString(data)
    searchResultsNode = response.getElementsByTagName('search')[0]
    numElems = int(searchResultsNode.getElementsByTagName('total_items')[0].childNodes[0].nodeValue)
    pageSize = int(searchResultsNode.getElementsByTagName('page_size')[0].childNodes[0].nodeValue)
    
    if numElems != 0:
        for i in range(pageSize):
            venuesNode = searchResultsNode.getElementsByTagName('venues')[0]
            venueNode = venuesNode.getElementsByTagName('venue')[i]
            
            viewTitleNode = venueNode.getElementsByTagName('venue_name')[0]
            names.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return names
    
#returns list of titles for events that match the query keywords
#this function supports backwards compatibility with previous versions
def getVenueName(keywords):
    return getVenueNameByLocation(keywords, '')
    
#get venue names by search query
def getVenueDescriptionByLocation(keywords, location):
    descriptions = []
    data = getResultsXMLLocation('venues', keywords, location)

    # parse the response data into a DOM
    response = parseString(data)
    searchResultsNode = response.getElementsByTagName('search')[0]
    numElems = int(searchResultsNode.getElementsByTagName('total_items')[0].childNodes[0].nodeValue)
    pageSize = int(searchResultsNode.getElementsByTagName('page_size')[0].childNodes[0].nodeValue)
    
    if numElems != 0:
        for i in range(pageSize):
            venuesNode = searchResultsNode.getElementsByTagName('venues')[0]
            venueNode = venuesNode.getElementsByTagName('venue')[i]
            
            viewTitleNode = venueNode.getElementsByTagName('description')[0]
            descriptions.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return descriptions
    
#returns list of titles for events that match the query keywords
#this function supports backwards compatibility with previous versions
def getVenueDescription(keywords):
    return getVenueDescriptionByLocation(keywords, '')

#get venue names by search query
def getVenueURLByLocation(keywords, location):
    urls = []
    data = getResultsXMLLocation('venues', keywords, location)

    # parse the response data into a DOM
    response = parseString(data)
    searchResultsNode = response.getElementsByTagName('search')[0]
    numElems = int(searchResultsNode.getElementsByTagName('total_items')[0].childNodes[0].nodeValue)
    pageSize = int(searchResultsNode.getElementsByTagName('page_size')[0].childNodes[0].nodeValue)
    
    if numElems != 0:
        for i in range(pageSize):
            venuesNode = searchResultsNode.getElementsByTagName('venues')[0]
            venueNode = venuesNode.getElementsByTagName('venue')[i]
            
            viewTitleNode = venueNode.getElementsByTagName('url')[0]
            urls.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return urls
    
#returns list of titles for events that match the query keywords
#this function supports backwards compatibility with previous versions
def getVenueURL(keywords):
    return getVenueURLByLocation(keywords, '')
    
#get venue names by search query
def getVenueCityByLocation(keywords, location):
    cities = []
    data = getResultsXMLLocation('venues', keywords, location)

    # parse the response data into a DOM
    response = parseString(data)
    searchResultsNode = response.getElementsByTagName('search')[0]
    numElems = int(searchResultsNode.getElementsByTagName('total_items')[0].childNodes[0].nodeValue)
    pageSize = int(searchResultsNode.getElementsByTagName('page_size')[0].childNodes[0].nodeValue)
    
    if numElems != 0:
        for i in range(pageSize):
            venuesNode = searchResultsNode.getElementsByTagName('venues')[0]
            venueNode = venuesNode.getElementsByTagName('venue')[i]
            
            viewTitleNode = venueNode.getElementsByTagName('city_name')[0]
            cities.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return cities
    
#returns list of titles for events that match the query keywords
#this function supports backwards compatibility with previous versions
def getVenueCity(keywords):
    return getVenueCityByLocation(keywords, '')
    
#get venue names by search query
def getVenueRegionByLocation(keywords, location):
    regions = []
    data = getResultsXMLLocation('venues', keywords, location)

    # parse the response data into a DOM
    response = parseString(data)
    searchResultsNode = response.getElementsByTagName('search')[0]
    numElems = int(searchResultsNode.getElementsByTagName('total_items')[0].childNodes[0].nodeValue)
    pageSize = int(searchResultsNode.getElementsByTagName('page_size')[0].childNodes[0].nodeValue)
    
    if numElems != 0:
        for i in range(pageSize):
            venuesNode = searchResultsNode.getElementsByTagName('venues')[0]
            venueNode = venuesNode.getElementsByTagName('venue')[i]
            
            viewTitleNode = venueNode.getElementsByTagName('region_name')[0]
            regions.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return regions
    
#returns list of titles for events that match the query keywords
#this function supports backwards compatibility with previous versions
def getVenueRegion(keywords):
    return getVenueRegionByLocation(keywords, '')
    
#get venue names by search query
def getVenueEventCountByLocation(keywords, location):
    eventCounts = []
    data = getResultsXMLLocation('venues', keywords, location)

    # parse the response data into a DOM
    response = parseString(data)
    searchResultsNode = response.getElementsByTagName('search')[0]
    numElems = int(searchResultsNode.getElementsByTagName('total_items')[0].childNodes[0].nodeValue)
    pageSize = int(searchResultsNode.getElementsByTagName('page_size')[0].childNodes[0].nodeValue)
    
    if numElems != 0:
        for i in range(pageSize):
            venuesNode = searchResultsNode.getElementsByTagName('venues')[0]
            venueNode = venuesNode.getElementsByTagName('venue')[i]
            
            viewTitleNode = venueNode.getElementsByTagName('event_count')[0]
            eventCounts.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return eventCounts
    
#returns list of titles for events that match the query keywords
#this function supports backwards compatibility with previous versions
def getVenueEventCount(keywords):
    return getVenueEventCountByLocation(keywords, '')
    
#get venues of events that match query
def getVenueIDs(keywords, location):
    eventCounts = []
    data = getResultsXMLLocation('venues', keywords, location)

    # parse the response data into a DOM
    response = parseString(data)
    searchResultsNode = response.getElementsByTagName('search')[0]
    numElems = int(searchResultsNode.getElementsByTagName('total_items')[0].childNodes[0].nodeValue)
    pageSize = int(searchResultsNode.getElementsByTagName('page_size')[0].childNodes[0].nodeValue)
    
    if numElems != 0:
        for i in range(pageSize):
            eventsNode = searchResultsNode.getElementsByTagName('venues')[0]
            eventNode = eventsNode.getElementsByTagName('venue')[i]
            
            viewTitleNode =eventNode.attributes['id']
            eventCounts.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return eventCounts
    
#returns array of URLS of images
def getVenueNameByID(id):
    names = []
    data = getResultsXMLByID('venues', id)
    # parse the response data into a DOM
    response = parseString(data)
    
    performerNode = response.getElementsByTagName('venue')[0]
    viewTitleNode = performerNode.getElementsByTagName('name')[0]
    if len(viewTitleNode.childNodes) != 0:
        names.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return names
    
    #returns array of URLS of images
def getVenueDescriptionByID(id):
    names = []
    data = getResultsXMLByID('venues', id)
    # parse the response data into a DOM
    response = parseString(data)
    
    performerNode = response.getElementsByTagName('venue')[0]
    viewTitleNode = performerNode.getElementsByTagName('description')[0]
    if len(viewTitleNode.childNodes) != 0:
        names.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return names
    
#returns array of URLS of images
def getVenueTypeByID(id):
    names = []
    data = getResultsXMLByID('venues', id)
    # parse the response data into a DOM
    response = parseString(data)
    
    performerNode = response.getElementsByTagName('venue')[0]
    viewTitleNode = performerNode.getElementsByTagName('venue_type')[0]
    if len(viewTitleNode.childNodes) != 0:
        names.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return names
    
#returns array of URLS of images
def getVenueAddressByID(id):
    names = []
    data = getResultsXMLByID('venues', id)
    # parse the response data into a DOM
    response = parseString(data)
    
    performerNode = response.getElementsByTagName('venue')[0]
    viewTitleNode = performerNode.getElementsByTagName('address')[0]
    if len(viewTitleNode.childNodes) != 0:
        names.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return names
    
#returns array of URLS of images
def getVenueCityByID(id):
    names = []
    data = getResultsXMLByID('venues', id)
    # parse the response data into a DOM
    response = parseString(data)
    
    performerNode = response.getElementsByTagName('venue')[0]
    viewTitleNode = performerNode.getElementsByTagName('city')[0]
    if len(viewTitleNode.childNodes) != 0:
        names.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return names
    
#returns array of URLS of images
def getVenueRegionByID(id):
    names = []
    data = getResultsXMLByID('venues', id)
    # parse the response data into a DOM
    response = parseString(data)
    
    performerNode = response.getElementsByTagName('venue')[0]
    viewTitleNode = performerNode.getElementsByTagName('region')[0]
    if len(viewTitleNode.childNodes) != 0:
        names.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return names
    
#returns array of URLS of images
def getVenueImagesByID(id):
    names = []
    data = getResultsXMLByID('venues', id)
    # parse the response data into a DOM
    response = parseString(data)
    
    performerNode = response.getElementsByTagName('venue')[0]
    imagesNode = performerNode.getElementsByTagName('images')[0]
    for i in range(len(performerNode.getElementsByTagName('images')[0].childNodes)):
        imageNode = imagesNode.getElementsByTagName('image')[i]
        viewTitleNode = performerNode.getElementsByTagName('url')[0]
        if len(viewTitleNode.childNodes) != 0:
            names.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return names
    
#returns array of event IDs occuring at that venue...can then use the ID to search for events
def getVenueEventsByID(id):
    names = []
    data = getResultsXMLByID('venues', id)
    # parse the response data into a DOM
    response = parseString(data)
    
    performerNode = response.getElementsByTagName('venue')[0]
    imagesNode = performerNode.getElementsByTagName('events')[0]
    for i in range(len(imagesNode.childNodes)):
        imageNode = imagesNode.getElementsByTagName('event')[i]
        viewTitleNode = performerNode.getElementsByTagName('id')[0]
        if len(viewTitleNode.childNodes) != 0:
            names.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return names

#get venues of events that match query
def getPerformerName(keywords):
    names = []
    data = getResultsXML('performers', keywords)

    # parse the response data into a DOM
    response = parseString(data)
    searchResultsNode = response.getElementsByTagName('search')[0]
    numElems = int(searchResultsNode.getElementsByTagName('total_items')[0].childNodes[0].nodeValue)
    pageSize = int(searchResultsNode.getElementsByTagName('page_size')[0].childNodes[0].nodeValue)
    
    if numElems != 0:
        for i in range(pageSize):
            venuesNode = searchResultsNode.getElementsByTagName('venues')[0]
            venueNode = venuesNode.getElementsByTagName('venue')[i]
            
            viewTitleNode = venueNode.getElementsByTagName('name')[0]
            names.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return names
    
#get venues of events that match query
def getPerformerURL(keywords):
    urls = []
    data = getResultsXML('performers', keywords)

    # parse the response data into a DOM
    response = parseString(data)
    searchResultsNode = response.getElementsByTagName('search')[0]
    numElems = int(searchResultsNode.getElementsByTagName('total_items')[0].childNodes[0].nodeValue)
    pageSize = int(searchResultsNode.getElementsByTagName('page_size')[0].childNodes[0].nodeValue)
    
    if numElems != 0:
        for i in range(pageSize):
            eventsNode = searchResultsNode.getElementsByTagName('performers')[0]
            eventNode = eventsNode.getElementsByTagName('performer')[i]
            
            viewTitleNode = eventNode.getElementsByTagName('url')[0]
            urls.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return urls
    
#get venues of events that match query
def getPerformerBio(keywords):
    bios = []
    data = getResultsXML('performers', keywords)

    # parse the response data into a DOM
    response = parseString(data)
    searchResultsNode = response.getElementsByTagName('search')[0]
    numElems = int(searchResultsNode.getElementsByTagName('total_items')[0].childNodes[0].nodeValue)
    pageSize = int(searchResultsNode.getElementsByTagName('page_size')[0].childNodes[0].nodeValue)
    
    if numElems != 0:
        for i in range(pageSize):
            eventsNode = searchResultsNode.getElementsByTagName('performers')[0]
            eventNode = eventsNode.getElementsByTagName('performer')[i]
            
            viewTitleNode = eventNode.getElementsByTagName('short_bio')[0]
            bios.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return bios
    
#get venues of events that match query
def getPerformerImage(keywords):
    images = []
    data = getResultsXML('performers', keywords)

    # parse the response data into a DOM
    response = parseString(data)
    searchResultsNode = response.getElementsByTagName('search')[0]
    numElems = int(searchResultsNode.getElementsByTagName('total_items')[0].childNodes[0].nodeValue)
    pageSize = int(searchResultsNode.getElementsByTagName('page_size')[0].childNodes[0].nodeValue)
    
    if numElems != 0:
        for i in range(pageSize):
            eventsNode = searchResultsNode.getElementsByTagName('performers')[0]
            eventNode = eventsNode.getElementsByTagName('performer')[i]
            
            viewTitleNode = eventNode.getElementsByTagName('image')[0]
            images.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return images
    
#get venues of events that match query
def getPerformerEventCount(keywords):
    eventCounts = []
    data = getResultsXML('performers', keywords)

    # parse the response data into a DOM
    response = parseString(data)
    searchResultsNode = response.getElementsByTagName('search')[0]
    numElems = int(searchResultsNode.getElementsByTagName('total_items')[0].childNodes[0].nodeValue)
    pageSize = int(searchResultsNode.getElementsByTagName('page_size')[0].childNodes[0].nodeValue)
    
    if numElems != 0:
        for i in range(pageSize):
            eventsNode = searchResultsNode.getElementsByTagName('performers')[0]
            eventNode = eventsNode.getElementsByTagName('performer')[i]
            
            viewTitleNode = eventNode.getElementsByTagName('event_count')[0]
            eventCounts.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return eventCounts
    
#get venues of events that match query
def getPerformerIDs(keywords):
    eventCounts = []
    data = getResultsXML('performers', keywords)

    # parse the response data into a DOM
    response = parseString(data)
    searchResultsNode = response.getElementsByTagName('search')[0]
    numElems = int(searchResultsNode.getElementsByTagName('total_items')[0].childNodes[0].nodeValue)
    pageSize = int(searchResultsNode.getElementsByTagName('page_size')[0].childNodes[0].nodeValue)
    
    if numElems != 0:
        for i in range(pageSize):
            eventsNode = searchResultsNode.getElementsByTagName('performers')[0]
            eventNode = eventsNode.getElementsByTagName('performer')[i]
            
            viewTitleNode = eventNode.getElementsByTagName('id')[0]
            eventCounts.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return eventCounts
    
def getPerformerNameByID(id):
    names = []
    data = getResultsXMLByID('performers', id)
    # parse the response data into a DOM
    response = parseString(data)
    
    performerNode = response.getElementsByTagName('performer')[0]
    viewTitleNode = performerNode.getElementsByTagName('name')[0]
    if len(viewTitleNode.childNodes) != 0:
        names.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return names
    
def getPerformerBioByID(id):
    names = []
    data = getResultsXMLByID('performers', id)
    # parse the response data into a DOM
    response = parseString(data)
    
    performerNode = response.getElementsByTagName('performer')[0]
    viewTitleNode = performerNode.getElementsByTagName('long_bio')[0]
    if len(viewTitleNode.childNodes) != 0:
        names.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return names
    
#returns array of URLS of images
def getPerformerImageByID(id):
    names = []
    data = getResultsXMLByID('performers', id)
    # parse the response data into a DOM
    response = parseString(data)
    
    performerNode = response.getElementsByTagName('performer')[0]
    imagesNode = response.getElementsByTagName('images')[0]
    imageNode = imagesNode.getElementsByTagName('image')[0]
    viewTitleNode = imageNode.getElementsByTagName('url')[0]
    if len(viewTitleNode.childNodes) != 0:
        names.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return names

#get list of events for a specified location
def getEventsByLocation(location):
    titles = []
    data = getResultsXMLLocation('events', '', location)

    # parse the response data into a DOM
    response = parseString(data)
    searchResultsNode = response.getElementsByTagName('search')[0]
    numElems = int(searchResultsNode.getElementsByTagName('total_items')[0].childNodes[0].nodeValue)
    pageSize = int(searchResultsNode.getElementsByTagName('page_size')[0].childNodes[0].nodeValue)
    
    if numElems != 0:
        for i in range(pageSize):
            eventsNode = searchResultsNode.getElementsByTagName('events')[0]
            eventNode = eventsNode.getElementsByTagName('event')[i]
            
            viewTitleNode = eventNode.getElementsByTagName('title')[0]
            titles.append(viewTitleNode.childNodes[0].nodeValue)

    response.unlink
    return titles
    
#   example of how to use functions
#def main(args):
    #***** Example of how to use arguments for keyword/location *****
    # print getEventTitles('')    #queries just by keyword
    # print getEventTitlesByLocation('', 'College+Station')   #queries just by location
    # print getEventTitlesByLocation('hsdhjksdflkdf', 'ajidasjhdfk')   #queries by keyword and location
    
    #***** Performer Functions *****
    #ids = getPerformerIDs('Bach')   #can't search by location for performers
    #print getPerformerNameByID(ids[0])
    #print getPerformerBioByID(ids[0])
    #print getPerformerImageByID(ids[0])

    #***** Event functions *****
    #ids = getEventIDs('', 'College+Station')    #gets all events at that location
    #print getEventTitleByID(ids[0])
    #print getEventTitleByID(ids[0])
    #print getEventDescriptionByID(ids[0])
    #print getEventStartTimeByID(ids[0])
    #print getEventVenueIDByID(ids[0])
    #print getEventImagesByID(ids[0])
    
    #***** Venue functions *****
    #ids = getVenueIDs('Bach', '')   #searches for keyword but not location
    #print getVenueNameByID(ids[0])
    #print getVenueDescriptionByID(ids[0])
    #print getVenueTypeByID(ids[0])
    #print getVenueAddressByID(ids[0])
    #print getVenueCityByID(ids[0])
    #print getVenueRegionByID(ids[0])
    #print getVenueImagesByID(ids[0])
    #print getVenueEventsByID(ids[0])
#if __name__ == "__main__":
#    import sys
#    main(sys.argv)
